package com.sky.service.impl;

import com.sky.constant.BaseConstant;
import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author : by smomo
 * @date : 2023/7/12 16:19
 * @description :
 */
@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private DishMapper dishMapper;

    @Override
    @Transactional
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        //1.查看自己购物车的数据
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(BaseContext.getCurrentId());
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);

        List<ShoppingCart> cartList=shoppingCartMapper.getByUserId(shoppingCart);
        if(cartList!=null&&cartList.size()==1){
            //1.1如果发现购物车存在相同的菜品或者套餐，则进行数量+1
            ShoppingCart cart= cartList.get(0);
            //数量加1
            cart.setNumber(cart.getNumber()+1);
            //更新购物车信息
            shoppingCartMapper.update(cart);
        }else {
            //2.判断当前加入购物车是菜品还是套餐
            Long setMealId = shoppingCartDTO.getSetmealId();
            if(setMealId!=null)
            {
                //插入的是套餐
                Setmeal setmeal = setmealMapper.getById(setMealId);
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
            }else {
                //插入的是菜品
                Dish dish = dishMapper.getById(shoppingCartDTO.getDishId());
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
            }
            //3.保存到数据库
            shoppingCart.setNumber(BaseConstant.DEFAULT_NUM);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.insert(shoppingCart);
        }


    }

    @Override
    public List<ShoppingCart> queryList() {
        //获取当前用户的id
        ShoppingCart shoppingCart = new ShoppingCart();
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);
        return shoppingCartMapper.query(shoppingCart);
    }

    @Override
    public void cleanShoppingCart() {
        Long userId = BaseContext.getCurrentId();
        shoppingCartMapper.clean(userId);
    }

    @Override
    public void subShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        //2.查找当前清单物品的数量，如果是1，删除该物品，否则，数量减1
        List<ShoppingCart> list = shoppingCartMapper.query(shoppingCart);
        shoppingCart = list.get(0);
        Integer number = shoppingCart.getNumber();
        if(number==1)
        {
            shoppingCartMapper.delete(shoppingCart);
        }else {
            shoppingCart.setNumber(shoppingCart.getNumber()-1);
        }
        shoppingCartMapper.update(shoppingCart);
    }


}
